import java.util.Random;
class Counter{
    public int sum = 0;
    public int get(){
        return sum;
    }
}
public class ThreadSum {
    public static void main(String[] args) throws InterruptedException {
        Object locker = new Object();
        int[] MyArray = new int[10000000];
        Random random = new Random();
        for (int i = 0; i < MyArray.length; i++) {
            MyArray[i] = random.nextInt(100)+1;
        }
        Counter counter = new Counter();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 10000000; i++) {
                if (i % 2 == 0){
                    synchronized (locker) {
                        counter.sum += MyArray[i];
                    }
                }
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 10000000; i++) {
                if (i % 2 != 0){
                    synchronized (locker){
                        counter.sum += MyArray[i];
                    }
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(counter.get());
    }

}
